/**
/* Copyright (C) 2011, Catdroid, Jordi Bernabeu
/* 
/* This program is free software; you can redistribute it and/or
/* modify it under the terms of the GNU General Public License
/* as published by the Free Software Foundation in its version 2
/* of the License.
/* 
/* This program is distributed in the hope that it will be useful,
/* but WITHOUT ANY WARRANTY; without even the implied warranty of
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
/* GNU General Public License for more details.
/*
/* You should have received a copy of the GNU General Public License
/* along with this program; if not, write to the Free Software
/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
/* 
/* @author Jordi Bernabeu (@androideverde)
 */

package org.catdroid.demolife;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

/**
 * /* Demo Life demostra el cicle de vida d'una activitat mostrant /* els
 * diferents events de canvi d'estat per on passa. /* Classe principal. La
 * classe deriva de Activity i alhora /* gestiona els events onClick.
 */
public class MainActivity extends Activity implements OnClickListener {

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// Cridem pel cicle de vida
		Shout("onCreate.");

		// Establim un Listener per capturar l'event onClick sobre cada botó
		Button btnDialog = (Button) findViewById(R.id.btnDialog);
		btnDialog.setOnClickListener(this);
		Button btnActivity = (Button) findViewById(R.id.btnActivity);
		btnActivity.setOnClickListener(this);
		Button btnExit = (Button) findViewById(R.id.btnExit);
		btnExit.setOnClickListener(this);
	}

	/** Called when the activity goes visible */
	@Override
	public void onStart() {
		// Cridem pel cicle de vida
		Shout("onStart.");
		super.onStart();
	}

	/** Called when the activity is about to be visible again */
	@Override
	public void onRestart() {
		// Cridem pel cicle de vida
		Shout("onReStart.");
		super.onRestart();
	}

	/** Called when the activity goes active */
	@Override
	public void onResume() {
		// Cridem pel cicle de vida
		Shout("onResume.");
		super.onResume();
	}

	/** Called when the activity goes away from being active */
	@Override
	public void onPause() {
		// Cridem pel cicle de vida
		Shout("onPause.");
		super.onPause();
	}

	/** Called when the activity goes behind a new active activity */
	@Override
	public void onStop() {
		// Cridem pel cicle de vida
		Shout("onStop.");
		super.onStop();
	}

	/** Called when the activity is about to be destroyed */
	@Override
	public void onDestroy() {
		// Cridem pel cicle de vida
		Shout("onDestroy.");
		super.onDestroy();
	}

	/** Crida un missatge tant per LogCat com per toast */
	public void Shout(String message) {
		// Publiquem el missatge per la finestra de LogCat
		Log.v("DemoLife", message);
		// Fa un toast curt mostrant el missatge
		Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
	}

	/**
	 * Estem obligats a implementar aquest mètode perquè la classe MainActivity
	 * implementa OnClickListener
	 */
	@Override
	public void onClick(View v) {
		// Depenent de quin View hagi rebut l'event onClick farem una cosa o
		// altra
		switch (v.getId()) {
		case R.id.btnDialog:
			// Cridem al dialog
			Intent i = new Intent(this, DialogActivity.class);
			startActivity(i);
			break;
		case R.id.btnActivity:
			// Cridem a l'altra activity
			Intent j = new Intent(this, AnotherActivity.class);
			startActivity(j);
			break;
		case R.id.btnExit:
			// Sortim de l'activity
			MainActivity.this.finish();
			break;
		}

	}
}